package com.example.himalaya2.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.himalaya2.R;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class PlayTrackPageAdapter extends PagerAdapter {

    private List<Track> mTracksList = new ArrayList<>();

    @Override
    public int getCount() {
        return mTracksList.size();
    }

    @NonNull
    @NotNull
    @Override
    public Object instantiateItem(@NonNull @NotNull ViewGroup container, int position) {

        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.item_track_pager,container,false);
        container.addView(itemView);
        // 设置数据
        // 找到控件
        ImageView item = itemView.findViewById(R.id.track_pager_item);
        // 设置图片
        Track track = mTracksList.get(position);
        String coverUrlLargeCover = track.getCoverUrlLarge();
        Picasso.with(container.getContext()).load(coverUrlLargeCover).into(item);
        return itemView;
    }

    @Override
    public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {

        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object object) {
        return view == object;
    }

    public void setData(List<Track> tracks) {
        mTracksList.clear();
        mTracksList = tracks;
        notifyDataSetChanged();
    }

}
